package com.aloda.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.aloda.constant.ExceptionConstant;
import com.aloda.entity.LoginLog;
import com.aloda.mapper.LoginLogMapper;
import com.aloda.query.LoginLogPageDTO;
import com.aloda.result.PageResult;
import com.aloda.service.LoginLogService;
import com.aloda.vo.LoginLogExportVO;
import com.aloda.vo.LoginLogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author aloda
* @description 针对表【login_log】的数据库操作Service实现
* @createDate 2024-12-18 18:28:40
*/
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {

    @Resource
    private LoginLogMapper loginLogMapper;

    /**
     * 分页查询
     *
     * @param loginLogPageDTO
     * @return
     */
    @Override
    public PageResult<LoginLogVO> pageQuery(LoginLogPageDTO loginLogPageDTO) {
        Page<LoginLog> page = loginLogPageDTO.toMpPage("login_time", false);
        LambdaQueryWrapper<LoginLog> loginLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        loginLogLambdaQueryWrapper
                .like(loginLogPageDTO.getLoginDesc()!=null,LoginLog::getLoginDesc,loginLogPageDTO.getLoginDesc())
                .like(loginLogPageDTO.getMethodParams()!=null,LoginLog::getMethodParams,loginLogPageDTO.getMethodParams())
                .between(loginLogPageDTO.getBeginTime()!=null&&loginLogPageDTO.getEndTime()!=null,
                        LoginLog::getLoginTime,loginLogPageDTO.getBeginTime(),loginLogPageDTO.getEndTime());
        loginLogMapper.selectPage(page,loginLogLambdaQueryWrapper);
        return PageResult.of(page,LoginLogVO.class);
    }

    /**
     * 导出登录日志信息到Excel
     * @param response HttpServletResponse
     * @param ids 登录日志ID列表
     */
    public void exportLoginLogToExcel(HttpServletResponse response, List<Long> ids) {
        try {
            // 设置响应头
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("登录日志信息表", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            // 查询所有信息 如果ids为空查询所有信息，否则查询对应信息
            LambdaQueryWrapper<LoginLog> queryWrapper = new LambdaQueryWrapper<>();
            List<LoginLog> loginLogList;
            if (ids.isEmpty()) {
                loginLogList = loginLogMapper.selectList(queryWrapper);
            } else {
                loginLogList = loginLogMapper.selectBatchIds(ids);
            }

            // 转换为导出VO
            List<LoginLogExportVO> exportVOList = loginLogList.stream()
                    .map(loginLog -> BeanUtil.copyProperties(loginLog, LoginLogExportVO.class)).toList();

            // 使用EasyExcel导出
            EasyExcel.write(response.getOutputStream(), LoginLogExportVO.class)
                    .sheet("登录日志信息")
                    .doWrite(exportVOList);

        } catch (IOException e) {
            throw new RuntimeException(ExceptionConstant.EXPORT_EXCEL_ERROR, e);
        }
    }
}




